﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

using WebAutomation.Browsers;

namespace WebAutomation
{
    public class TestCommand:CommandBase
    {
        public TestCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);
        }

        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref System.Data.DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            string command = parameter[0];
            testLineResult.LineCommand = command;
            testLineResult.LineParameter = parameter[1];
            testLineResult.LineSubCommand = command;
            if (this.BrowserObject == null)
            {
                this.BrowserObject = browserObject;
            }
            try
            {

                switch (command.ToLower())
                {
                    case "exec":
                        string appToRun = "";
                        string param = "";
                        string testType = "";
                        bool isValid = false;
                        try
                        {
                            testType = parameter[1].ToLower();
                            isValid = true;
                        }catch
                        {

                            throw new Exception("The Test Type parameter is unknown.You must pass through the following options : Internal or NUnitTest");
                        }
                        if(isValid)
                        {
                            if (string.IsNullOrEmpty(parameter[1]))
                            {
                                throw new Exception("The Test Type parameter is unknown.You must pass through the following options : Internal or NUnitTest");
                            }
                            else
                            {
                                switch (testType)
                                {
                                    case "internal":
                                        WebAuto webauto = new WebAuto(null, parameter[2]);


                                        webauto.RunTestNonThreaded(testDataExceptionRules, this.BrowserObject.ProcessID);
                                        break;

                                    default:
                                        throw new Exception("The Test Type parameter is unknown.You must pass through the following options : Internal or NUnitTest");
                                        break;
                                }

                            }
                        }


                        // RunApp(appToRun, param);
                        testLineResult.LinePassed = true;
                        break;
                    default:
                        testLineResult.IsFrameworkError = false;
                        testLineResult.LineMessage = "Error.The sub command " + command + " is unknown.";
                        testLineResult.LinePassed = false;
                        break;

                }






            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message + "." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }

            return testLineResult.LinePassed;
        }
    }
}
